package com.ssyc.common.quanzi;

import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ssyc.common.R;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.Constants;
import com.ssyc.common.base.NineImageLoader;
import com.ssyc.common.base.SpKeys;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.bean.ResultInfo;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.manager.StateBarManager;
import com.ssyc.common.utils.AlertDialogUtil;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.KeyBoardUtils;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.dialog.CustomDialogManager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 圈子详情界面
 * Created by Administrator on 2018/7/11 0011.
 */

public class PersonCircleDetailActivity extends BaseActivity implements View.OnClickListener, View.OnLayoutChangeListener, TextWatcher {

    private RelativeLayout rlback;
    private RecyclerView rv;

    private SmartRefreshLayout srl;

    private RelativeLayout rlLoading;

    private RelativeLayout rlContent;

    private QuanZiInfo.CirclelistBean info;

    private List<QuanZiInfo.CirclelistBean.CommentlistBean> oldDatas;

    private LinearLayout llContent;

    private EditText etInput;

    private Button btSend;

    //Activity最外层的Layout视图
    private View activityRootView;


    //屏幕高度
    private int screenHeight = 0;

    //软件盘弹起后所占高度阀值
    private int keyHeight = 0;

    //头布局
    private View headView;

    //空视图
    private View emptyView;

    //适配器
    private CircleDetailsRvAdapter adapter;

    //头布局的相关控件
    private ImageView ivHead;
    private TextView tvName;
    private TextView tvContent;
    private NineGridView nineGridView;
    private TextView tvTime;
    private TextView tvDelete;
    private ImageView lvComment;
    private LinearLayout llLike;
    private TextView tvLike;

    //弹框相关的PopUpWindow
    private PopupWindow popupWindow;
    private View view;
    private TextView tvZan, tvPl, tvZf;

    //区别是否是评论或者回复的标识
    private int type = 1;

    //记录回复的位置
    private int pos;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.base_activity_person_circle_details;
    }

    @Override
    protected void init() {

        showContent();

        //配置状态栏颜色
        initStateColor();

        //初始化视图
        initView();

        //获取传递的对象
        initIntent();

        //初始化下拉刷新
        initSrf();

        //设置列表
        initRv();

    }


    /**
     * 初始化视图
     */
    public void initView() {
        rlback = findViewById(R.id.rlback);
        rlback.setOnClickListener(this);
        rv = findViewById(R.id.rv);
        srl = findViewById(R.id.srl);
        rlLoading = findViewById(R.id.rl_loading);
        rlContent = findViewById(R.id.rl_content);
        rlLoading.setVisibility(View.GONE);

        etInput = findViewById(R.id.et_input);
        etInput.addTextChangedListener(this);
        btSend = findViewById(R.id.bt_send);
        btSend.setOnClickListener(this);
        llContent = findViewById(R.id.ll_content);
        llContent.setOnClickListener(this);
        activityRootView = findViewById(R.id.rl_main);
    }


    /**
     * 配置状态栏颜色的方法
     */
    private void initStateColor() {
        //设置6.0以后沉浸状体栏的问题
        StateBarManager.setBarWithUltimateBar(this, getResources().getColor(com.ssyc.common.R.color.titlebg),
                getResources().getColor(com.ssyc.common.R.color.nav_color));
    }

    /**
     * 初始化下拉刷新控件
     */
    private void initSrf() {
        //配置下拉刷新控件
        SrfManager.setSmartByPullListener(srl, null);
    }


    /**
     * 获取传递的对象
     */
    private void initIntent() {
        info = (QuanZiInfo.CirclelistBean) getIntent().getSerializableExtra("data");
    }

    /**
     * 初始化列表控件
     */
    private void initRv() {

        headView = View.inflate(this, R.layout.base_rv_head_circle_details, null);

        //头布局的相关控件
        //头像
        ivHead = headView.findViewById(R.id.lv_head);

        //姓名
        tvName = headView.findViewById(R.id.tv_name);

        //评论内容
        tvContent = headView.findViewById(R.id.tv_content);

        //九宫格显示图片控件
        nineGridView = headView.findViewById(R.id.ninegv);

        //时间
        tvTime = headView.findViewById(R.id.tv_time);

        //删除
        tvDelete = headView.findViewById(R.id.tv_delete);
        if (1 == SPUtil.getInt(this, SpKeys.CIRCLETYPE)) {
            //证明是自己的头像点击进入的朋友圈详情,显示删除按钮
            tvDelete.setVisibility(View.VISIBLE);
        } else if (2 == SPUtil.getInt(this, SpKeys.CIRCLETYPE)) {
            tvDelete.setVisibility(View.GONE);
        }
        tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //删除自己的评论
                AlertDialogUtil.showAlertDialog(PersonCircleDetailActivity.this, "确定删除吗?", new AlertDialogUtil.onClickListener() {
                    @Override
                    public void onClickOk() {
                        doDeleteCircleAction();
                    }
                });
            }
        });

        //点击弹框的评论图片
        lvComment = headView.findViewById(R.id.lv_comment);
        lvComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //弹出点赞,点赞,转发的PopUpWindow
                showPopWindow();
            }
        });

        //点赞的整体布局
        llLike = headView.findViewById(R.id.ll_like);

        //显示点赞的人的文字
        tvLike = headView.findViewById(R.id.tv_like_name);

        //通过传递过来的评论对象设置头布局的数据
        setHeadViewData();

        //设置列表适配器
        oldDatas = info.getCommentlist();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty, null);
        adapter = new CircleDetailsRvAdapter(this, R.layout.base_item_rv_comment, oldDatas);
        adapter.addHeaderView(headView);
        BqaManager.setRv(emptyView, this, adapter, rv);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {
                //条目点击事件
                if (oldDatas.get(position).getUser_name().equals(SPUtil.getString(PersonCircleDetailActivity.this, SpKeys.NAME))) {
                    //标识点击的是自己回复的评论,则弹出弹出的对话框,点击删除后删除该条记录
                    AlertDialogUtil.showAlertDialog(PersonCircleDetailActivity.this, "确定删除该条评论内容吗?", new AlertDialogUtil.onClickListener() {
                        @Override
                        public void onClickOk() {
                            doDeletCommentAction(position);
                        }
                    });

                } else {
                    //弹出软键盘顶起底部布局
                    type = 2;
                    pos = position;
                    llContent.setVisibility(View.VISIBLE);
                    etInput.requestFocus();
                    etInput.setHint("回复" + oldDatas.get(position).getUser_name());
                    if (oldDatas != null && oldDatas.size() != 0) {
                        rv.smoothScrollToPosition(oldDatas.size() - 1);
                    }
                    KeyBoardUtils.openKeybord(etInput);
                }
            }
        });

    }


    /**
     * 删除自己朋友圈的方法
     */
    private void doDeleteCircleAction() {
        CustomDialogManager.show(this, "删除中...");
        Map<String, String> map = new HashMap<>();
        map.put("type", "13");
        map.put("acc", info.getUser_id() + "");
        map.put("role", info.getRole() + "");
        map.put("circlerowid", info.getRow_id() + "");
        map.put("platform", "8");
        HttpUtils.post(HttpAdress.DELETECIRLESSElf, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                CustomDialogManager.dissmiss();
                UiUtils.Toast(BaseApplication.ERROR + ",删除失败", false);
            }

            @Override
            public void onResponse(String response, int id) {
                CustomDialogManager.dissmiss();
                if (!TextUtils.isEmpty(response)) {
                    ResultInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            //第一步:通知朋友圈界面进行刷新操作
                            BusInfo busInfo = new BusInfo();
                            busInfo.setType(MyQuanziActivity.UPDATE);
                            EventBus.getDefault().post(busInfo);
                            //第二步:通知个人相册界面进行刷新操作
                            BusInfo newInfo = new BusInfo();
                            newInfo.setType(PersonCircleActivity.UPDATE);
                            EventBus.getDefault().post(newInfo);

                            //关闭当前页面即可
                            finish();

                        } else {
                            UiUtils.Toast("删除失败" + Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });

    }

    /**
     * 删除评论的操作
     *
     * @param position
     */
    private void doDeletCommentAction(final int position) {
        CustomDialogManager.show(this, "删除中...");
        Map<String, String> map = new HashMap<>();
        map.put("type", "8");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("circlerowid", info.getRow_id() + "");
        map.put("platform", "8");
        map.put("comment_id", oldDatas.get(position).getComment_id() + "");
        HttpUtils.post(HttpAdress.ZFCIRCLE, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                CustomDialogManager.dissmiss();
                UiUtils.Toast(BaseApplication.ERROR + ",删除失败", false);
            }

            @Override
            public void onResponse(String response, int id) {
                CustomDialogManager.dissmiss();
                if (!TextUtils.isEmpty(response)) {
                    ResultInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
//                            UiUtils.Toast("删除成功", false);
                            //第一步:通知朋友圈界面进行刷新操作
                            BusInfo busInfo = new BusInfo();
                            busInfo.setType(MyQuanziActivity.UPDATE);
                            EventBus.getDefault().post(busInfo);
                            //第二步:通知个人相册界面进行刷新操作
                            BusInfo newInfo = new BusInfo();
                            newInfo.setType(PersonCircleActivity.UPDATE);
                            EventBus.getDefault().post(newInfo);

                            //更新适配器
                            oldDatas.remove(position);
                            adapter.notifyDataSetChanged();
                        } else {
                            UiUtils.Toast("删除失败" + Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });

    }


    /**
     * 弹出点赞,点赞,转发的PopUpWindow
     */
    private void showPopWindow() {

        //点击评论弹出评论的PopUpWindow
        view = View.inflate(PersonCircleDetailActivity.this, R.layout.common_pop_comment, null);
        tvZan = view.findViewById(R.id.tv_zan);
        //获取所有点赞的名称
        String likeNames = tvLike.getText().toString().trim();
        if (likeNames.contains(SPUtil.getString(this, SpKeys.NAME))) {
            //自己已经点错赞
            tvZan.setText("取消");
        } else {
            tvZan.setText("赞");
        }
        tvPl = view.findViewById(R.id.tv_pinglun);
        tvZf = view.findViewById(R.id.tv_zhuanfa);
        //点赞的点击事件
        tvZan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                //点赞
                if ("赞".equals(tvZan.getText().toString().trim())) {
                    doDzAction();
                } else if ("取消".equals(tvZan.getText().toString().trim())) {
                    doCancelDzAction();
                }


            }
        });
        //评论的点击事件
        tvPl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
                type = 1;
                //弹出软键盘顶起底部布局
                llContent.setVisibility(View.VISIBLE);
                etInput.requestFocus();
                KeyBoardUtils.openKeybord(etInput);
                if (oldDatas == null || oldDatas.size() == 0) {
                    rv.smoothScrollToPosition(1);
                } else {
                    rv.smoothScrollToPosition(oldDatas.size() - 1);
                }

            }
        });
        //转发的点击事件
        tvZf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                //转发
                doZfAction();
            }
        });
        popupWindow = new PopupWindow(view, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int mShowMorePopupWindowWidth = view.getMeasuredWidth();
        int mShowMorePopupWindowHeight = view.getMeasuredHeight();

        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        } else {
            int heightlvComment = lvComment.getHeight();

            popupWindow.showAsDropDown(lvComment, -mShowMorePopupWindowWidth - 20,
                    -(mShowMorePopupWindowHeight + heightlvComment) / 2);
        }
    }

    /**
     * 点赞的操作
     */
    private void doDzAction() {
        CustomDialogManager.show(this, "点赞中...");
        Map<String, String> map = new HashMap<>();
        map.put("type", "3");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("circlerowid", info.getRow_id() + "");
        map.put("platform", "8");

        HttpUtils.post(HttpAdress.CLICKZAN, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                CustomDialogManager.dissmiss();
                UiUtils.Toast(BaseApplication.ERROR + ",点赞失败", false);
            }

            @Override
            public void onResponse(String response, int id) {
                CustomDialogManager.dissmiss();
                if (!TextUtils.isEmpty(response)) {
                    ResultInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
//                            UiUtils.Toast("点赞成功", false);

                            //第一:更新当前头布局的点赞内容
                            UpdateHeadViewByZan();

                            //第二步:通知朋友圈界面进行刷新操作
                            BusInfo busInfo = new BusInfo();
                            busInfo.setType(MyQuanziActivity.UPDATE);
                            EventBus.getDefault().post(busInfo);

                            //第三步:通知个人相册界面进行刷新操作
                            BusInfo newInfo = new BusInfo();
                            newInfo.setType(PersonCircleActivity.UPDATE);
                            EventBus.getDefault().post(newInfo);
                        } else {
                            UiUtils.Toast("点赞失败" + Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });

    }

    /**
     * 取消点赞的方法
     */
    private void doCancelDzAction() {
        CustomDialogManager.show(this, "点赞中...");
        Map<String, String> map = new HashMap<>();
        map.put("type", "4");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("circlerowid", info.getRow_id() + "");
        map.put("platform", "8");
        HttpUtils.post(HttpAdress.CLICKZAN, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                CustomDialogManager.dissmiss();
                UiUtils.Toast(BaseApplication.ERROR + ",取消赞失败", false);
            }

            @Override
            public void onResponse(String response, int id) {
                CustomDialogManager.dissmiss();
                if (!TextUtils.isEmpty(response)) {
                    ResultInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            UiUtils.Toast("取消赞成功", false);
                            //第一:更新当前头布局的点赞内容
                            UpdateHeadViewByCancelZan();
                            //第二步:通知朋友圈界面进行刷新操作
                            BusInfo busInfo = new BusInfo();
                            busInfo.setType(MyQuanziActivity.UPDATE);
                            EventBus.getDefault().post(busInfo);

                            //第三步:通知个人相册界面进行刷新操作
                            BusInfo newInfo = new BusInfo();
                            newInfo.setType(PersonCircleActivity.UPDATE);
                            EventBus.getDefault().post(newInfo);

                        } else {
                            UiUtils.Toast("取消赞失败" + Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }

    /**
     * 转发
     */
    private void doZfAction() {
        CustomDialogManager.show(this, "转发中...");
        Map<String, String> map = new HashMap<>();
        map.put("type", "14");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("circlerowid", info.getRow_id() + "");
        map.put("platform", "8");
        map.put("acc2", info.getUser_id() + "");
        map.put("role2", info.getRole() + "");
        map.put("usernickname2", info.getUser_name());
        HttpUtils.post(HttpAdress.ZFCIRCLE, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                CustomDialogManager.dissmiss();
                UiUtils.Toast(BaseApplication.ERROR + ",转发失败", false);
            }

            @Override
            public void onResponse(String response, int id) {
                CustomDialogManager.dissmiss();
                if (!TextUtils.isEmpty(response)) {
                    ResultInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            UiUtils.Toast("转发成功", false);
                            //第一步:通知朋友圈界面进行刷新操作
                            BusInfo busInfo = new BusInfo();
                            busInfo.setType(MyQuanziActivity.UPDATE);
                            EventBus.getDefault().post(busInfo);
                            //第二步:通知个人相册界面进行刷新操作
                            BusInfo newInfo = new BusInfo();
                            newInfo.setType(PersonCircleActivity.UPDATE);
                            EventBus.getDefault().post(newInfo);
                        } else {
                            UiUtils.Toast("转发失败" + Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }


    /**
     * 发送评论内容的方法
     */
    private void sendCommentAction() {
        //获取输入内容
        final String inputText = etInput.getText().toString().trim();
        if (TextUtils.isEmpty(inputText)) {
            UiUtils.Toast("未输入评论内容", false);
            return;
        } else {

            //第三步:更新当前界面
            KeyBoardUtils.closeKeyboard(etInput);
            etInput.setText("");

            //隐藏评论布局
            llContent.setVisibility(View.GONE);

            CustomDialogManager.show(this, "发布中...");
            Map<String, String> map = new HashMap<>();
            if (type == 1) {
                //发送评论
                map.put("type", "6");
                map.put("acc", AccountUtils.getAccount(this));
                map.put("role", AccountUtils.getRole(this));
                map.put("circlerowid", info.getRow_id() + "");
                map.put("platform", "8");
                map.put("content", inputText);
            } else if (type == 2) {
                //回复评论
                map.put("type", "7");
                map.put("acc", AccountUtils.getAccount(this));
                map.put("role", AccountUtils.getRole(this));
                map.put("circlerowid", info.getRow_id() + "");
                map.put("platform", "8");
                map.put("content", inputText);
                map.put("acc2", oldDatas.get(pos).getUser_id());
                map.put("role2", oldDatas.get(pos).getRole() + "");
            }

            HttpUtils.post(HttpAdress.COMMENT, map, this, new HttpUtils.CallBack() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    CustomDialogManager.dissmiss();
                    UiUtils.Toast(BaseApplication.ERROR + ",发布失败", false);
                }

                @Override
                public void onResponse(String response, int id) {
                    Log.i("test", response + "---评论的返回值");
                    CustomDialogManager.dissmiss();
                    if (!TextUtils.isEmpty(response)) {
                        CommentInfo info = null;
                        try {
                            info = GsonUtil.jsonToBean(response, CommentInfo.class);
                        } catch (Exception e) {
                            Log.i("test", Constants.PARSEDATAERROR);
                        }
                        if (null != info) {
                            if ("200".equals(info.getState())) {
                                //返回成功
//                                UiUtils.Toast("发布成功", false);
                                //第一步:通知朋友圈界面进行刷新操作
                                BusInfo busInfo = new BusInfo();
                                busInfo.setType(MyQuanziActivity.UPDATE);
                                EventBus.getDefault().post(busInfo);

                                //第二步:通知个人相册界面进行刷新操作
                                BusInfo newInfo = new BusInfo();
                                newInfo.setType(PersonCircleActivity.UPDATE);
                                EventBus.getDefault().post(newInfo);

                                //更新评论列表
                                QuanZiInfo.CirclelistBean.CommentlistBean commentlistBean = new QuanZiInfo.CirclelistBean.CommentlistBean();
                                if (type == 1) {
                                    //发送评论
                                    commentlistBean.setUser_name(SPUtil.getString(PersonCircleDetailActivity.this, SpKeys.NAME));
                                    commentlistBean.setContent(inputText);
                                    commentlistBean.setComment_id(Integer.parseInt(info.getComment_id()));
                                } else if (type == 2) {
                                    //回复评论
                                    commentlistBean.setUser_name(SPUtil.getString(PersonCircleDetailActivity.this, SpKeys.NAME));
                                    commentlistBean.setUser_name2(oldDatas.get(pos).getUser_name());
                                    commentlistBean.setContent(inputText);
                                    commentlistBean.setComment_id(Integer.parseInt(info.getComment_id()));
                                }
                                oldDatas.add(0, commentlistBean);
                                adapter.notifyDataSetChanged();

                            } else {
                                UiUtils.Toast("发布失败" + Constants.ERRORSTATE + info.getState(), false);
                                Log.i("test", Constants.ERRORSTATE + info.getState());
                            }

                        } else {
                            Log.i("test", Constants.EMPTY);
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }
                }
            });

        }


    }


    /**
     * 点赞成功后更新头布局的数据
     */
    private void UpdateHeadViewByZan() {
        if (llLike.getVisibility() == View.GONE) {
            llLike.setVisibility(View.VISIBLE);
            tvLike.setText(SPUtil.getString(this, SpKeys.NAME));
        } else {
            String content = tvLike.getText().toString().trim();
            tvLike.setText(content + "," + SPUtil.getString(this, SpKeys.NAME));
        }
    }

    /**
     * 取消赞成功后更新头布局的数据
     */
    private void UpdateHeadViewByCancelZan() {
        String likeNames = tvLike.getText().toString().trim();
        String ownName = SPUtil.getString(this, SpKeys.NAME);
        String deletNames = "";
        if (!likeNames.startsWith(ownName) && likeNames.endsWith(ownName)) {
            //以要删除的字符串开头
            deletNames = "," + ownName;
        } else if (likeNames.startsWith(ownName) && !likeNames.endsWith(ownName)) {
            deletNames = ownName;
        } else if (likeNames.startsWith(ownName) && likeNames.endsWith(ownName)) {
            deletNames = ownName;
        } else {
            deletNames = "," + ownName;
        }
        //做删除字符串的操作
        String resString = likeNames.replaceAll(deletNames, "");
        if (TextUtils.isEmpty(resString)) {
            //删除后没有点赞人数时
            tvLike.setText("");
            llLike.setVisibility(View.GONE);
        } else {
            llLike.setVisibility(View.VISIBLE);
            tvLike.setText(resString);
        }
    }

    /**
     * 设置头布局的数据
     */
    private void setHeadViewData() {
        if (null != info) {
            //头像
            ImageUtil.displayImage(this, ivHead, info.getUser_pic());
            //姓名
            tvName.setText(info.getUser_name());

            //评论内容
            tvContent.setText(info.getContent());

            //九宫格图片
            ArrayList<ImageInfo> imageInfos = new ArrayList<>();

            if (!TextUtils.isEmpty(info.getS_user_picture1())) {
                ImageInfo imageInfo = new ImageInfo();
                //缩略图
                imageInfo.setThumbnailUrl(info.getS_user_picture1());
                //大图
                imageInfo.setBigImageUrl(info.getUser_picture1());
                imageInfos.add(imageInfo);
            }
            if (!TextUtils.isEmpty(info.getS_user_picture2())) {
                ImageInfo imageInfo = new ImageInfo();
                //缩略图
                imageInfo.setThumbnailUrl(info.getS_user_picture2());
                //大图
                imageInfo.setBigImageUrl(info.getUser_picture2());
                imageInfos.add(imageInfo);
            }
            if (!TextUtils.isEmpty(info.getS_user_picture3())) {
                ImageInfo imageInfo = new ImageInfo();
                //缩略图
                imageInfo.setThumbnailUrl(info.getS_user_picture3());
                //大图
                imageInfo.setBigImageUrl(info.getUser_picture3());
                imageInfos.add(imageInfo);
            }
            if (!TextUtils.isEmpty(info.getS_user_picture4())) {
                ImageInfo imageInfo = new ImageInfo();
                //缩略图
                imageInfo.setThumbnailUrl(info.getS_user_picture4());
                //大图
                imageInfo.setBigImageUrl(info.getUser_picture4());
                imageInfos.add(imageInfo);
            }
            if (!TextUtils.isEmpty(info.getS_user_picture5())) {
                ImageInfo imageInfo = new ImageInfo();
                //缩略图
                imageInfo.setThumbnailUrl(info.getS_user_picture5());
                //大图
                imageInfo.setBigImageUrl(info.getUser_picture5());
                imageInfos.add(imageInfo);
            }
            if (!TextUtils.isEmpty(info.getS_user_picture6())) {
                ImageInfo imageInfo = new ImageInfo();
                //缩略图
                imageInfo.setThumbnailUrl(info.getS_user_picture6());
                //大图
                imageInfo.setBigImageUrl(info.getUser_picture6());
                imageInfos.add(imageInfo);
            }
            if (!TextUtils.isEmpty(info.getS_user_picture7())) {
                ImageInfo imageInfo = new ImageInfo();
                //缩略图
                imageInfo.setThumbnailUrl(info.getS_user_picture7());
                //大图
                imageInfo.setBigImageUrl(info.getUser_picture7());
                imageInfos.add(imageInfo);
            }
            if (!TextUtils.isEmpty(info.getS_user_picture8())) {
                ImageInfo imageInfo = new ImageInfo();
                //缩略图
                imageInfo.setThumbnailUrl(info.getS_user_picture8());
                //大图
                imageInfo.setBigImageUrl(info.getUser_picture8());
                imageInfos.add(imageInfo);
            }
            if (!TextUtils.isEmpty(info.getS_user_picture9())) {
                ImageInfo imageInfo = new ImageInfo();
                //缩略图
                imageInfo.setThumbnailUrl(info.getS_user_picture9());
                //大图
                imageInfo.setBigImageUrl(info.getUser_picture9());
                imageInfos.add(imageInfo);
            }

            if (imageInfos.size() == 0) {
                nineGridView.setVisibility(View.GONE);
            } else {
                nineGridView.setVisibility(View.VISIBLE);
                //设置数据
                nineGridView.setImageLoader(new NineImageLoader());
                nineGridView.setAdapter(new NineGridViewClickAdapter(this, imageInfos));
            }

            //初次进来显示点赞布局的状态
            if (info.getLikelist() == null || info.getLikelist().size() == 0) {
                //没有点赞
                llLike.setVisibility(View.GONE);
            } else {
                llLike.setVisibility(View.VISIBLE);
                //获取文字
                StringBuffer sb = new StringBuffer();
                List<QuanZiInfo.CirclelistBean.LikelistBean> likelist = info.getLikelist();
                for (int i = 0; i < likelist.size(); i++) {
                    sb.append(likelist.get(i).getUser_name());
                    sb.append(",");
                }
                String likeNames = sb.toString();
                tvLike.setText(likeNames.substring(0, likeNames.length() - 1));
            }
        }

    }

    /**
     * 加入对根据局的管理监听,主要是解决软键盘弹出和关闭的监听状态
     */
    @Override
    protected void onResume() {
        super.onResume();

        //添加layout大小发生改变监听器
        activityRootView.addOnLayoutChangeListener(this);
    }

    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rlback) {
            //返回
            finish();
        } else if (id == R.id.bt_send) {
            //发送评论的按钮
            sendCommentAction();
        }
    }

    /**
     * 监听软键盘
     */
    @Override
    public void onLayoutChange(View v, int left, int top, int right,
                               int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

        //现在认为只要控件将Activity向上推的高度超过了1/3屏幕高，就认为软键盘弹起
        if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {
            if (llContent != null && llContent.getVisibility() == View.GONE) {
                llContent.setVisibility(View.VISIBLE);
            }

        } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {
            if (llContent != null && llContent.getVisibility() == View.VISIBLE) {
                llContent.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 评论框的输入框
     *
     * @param charSequence
     * @param i
     * @param i1
     * @param i2
     */
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        //获取输入内容
        String inputText = etInput.getText().toString().trim();
        if (TextUtils.isEmpty(inputText)) {
            btSend.setBackgroundResource(R.drawable.base_bt_no_send);
            btSend.setTextColor(getResources().getColor(R.color.unsend));
            btSend.setEnabled(false);
        } else {
            btSend.setBackgroundResource(R.drawable.base_bt_send);
            btSend.setTextColor(getResources().getColor(R.color.white));
            btSend.setEnabled(true);
        }
    }
}
